package cn.darkhorse.workflow.service.impl;


import cn.darkhorse.core.consts.BpmnBusinessConsts;
import cn.darkhorse.core.param.IUser;
import cn.darkhorse.workflow.auto.entity.BpmnDeptRelative;
import cn.darkhorse.workflow.auto.entity.BpmnSysUser;
import cn.darkhorse.workflow.auto.service.IBpmnDeptRelativeService;
import cn.darkhorse.workflow.auto.service.IBpmnSysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: mr.wang
 * @time: 2020/6/4 0004 12:00
 */
@Component("TaskRelativePostImpl")
public class TaskRelativePostImpl implements IUser {
	

	@Autowired
	IBpmnSysUserService iSysUserService;
	@Autowired
	IBpmnDeptRelativeService iDeptRelativeService;
	
	
	
	
	@Override
	public List<String> getUsers(String agrs,String relative) {
		List<String> list = new ArrayList<>();
		LambdaQueryWrapper<BpmnSysUser> params = new LambdaQueryWrapper<>();
		params.eq(BpmnSysUser::getUserName,agrs);
		BpmnSysUser sysUser = iSysUserService.getOne(params);
		if(sysUser!=null&& sysUser.getDeptId()>0){
			QueryWrapper<BpmnDeptRelative> wrapper = new QueryWrapper<>();
			wrapper.eq("dept_id",sysUser.getDeptId());
			BpmnDeptRelative deptRelative = iDeptRelativeService.getOne(wrapper);
			if(null == deptRelative){
				//throw new InteractionFailedException("流程相对岗位不存在");
			}
				if(BpmnBusinessConsts.RELATIVE_POST_CODE_DEPARTMENT_MANAGER.equals(relative)){
					Long relativeManager = deptRelative.getRelativeManager();
					if(null == relativeManager){
						//throw new InteractionFailedException("流程相对岗位的部门经理未设置！");
					}else{
						list.add(String.valueOf(iSysUserService.getById(relativeManager).getUserName()));
					}
				}else{
					Long relativeInCharge = deptRelative.getRelativeInCharge();
					if(null == relativeInCharge){
						//throw new InteractionFailedException("流程相对岗位的分管领导未设置！");
					}else{
						list.add(String.valueOf(iSysUserService.getById(relativeInCharge).getUserName()));
					}
				}
		}
		return list;
		/*String[] strings = agrs.split(",");
		String paramValue = strings[0];
		String deptId = strings[1];
		List<String> list = new ArrayList<>();
		if(deptId!=null){
			QueryWrapper<BpmnDeptRelative> wrapper = new QueryWrapper<>();
			wrapper.eq("dept_id",deptId);
			BpmnDeptRelative deptRelative = iDeptRelativeService.getOne(wrapper);
			if(deptRelative!=null){
				if(BpmnBusinessConsts.RELATIVE_POST_CODE_DEPARTMENT_MANAGER.equals(paramValue)){
					list.add(String.valueOf(iSysUserService.getById(deptRelative.getRelativeManager()).getUserName()));
				}else{
					list.add(String.valueOf(iSysUserService.getById(deptRelative.getRelativeInCharge()).getUserName()));
				}
			}
		}
		return list;*/
	}
	

}
